package com.msb.mall.product.controller;

import com.msb.common.utils.PageUtils;
import com.msb.common.vo.CommonPageReq;
import com.msb.common.vo.CommonResult;
import com.msb.mall.core.entity.product.SpuInfoEntity;
import com.msb.mall.core.vo.product.SpuInfoVO;
import com.msb.mall.product.service.SpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * spu信息
 *
 * @author leixu
 * @email xuleiwolf@163.com
 * @date 2024-04-19 20:08:58
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 列表
     */
    @PostMapping("/page")
    public CommonResult page(@RequestBody CommonPageReq<Map<String, Object>> req) {
        return CommonResult.success(spuInfoService.queryPageCondition(req));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public CommonResult list(@RequestParam Map<String, Object> params) {
        PageUtils page = spuInfoService.queryPage(params);

        return CommonResult.success(null, page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public CommonResult info(@PathVariable("id") Long id) {
        SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return CommonResult.success(null, spuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    public CommonResult save(@RequestBody SpuInfoEntity spuInfo) {
    public CommonResult save(@RequestBody SpuInfoVO spuInfoVo) {
//        spuInfoService.save(spuInfo);
        spuInfoService.saveSpuInfo(spuInfoVo);

        return CommonResult.success();
    }

    /**
     * app/product/spuinfo/6/up
     * 商品的上架功能
     * 传递过来一个spuID
     * 我们就需要根据SPUID查询出需要存储在ElasticSearch中的数据
     * 然后把数据存储到ELasticSearch中，并修改该SPU的状态为上架
     */
    @PostMapping("/{spuId}/up")
    public CommonResult spuUp(@PathVariable("spuId") Long spuId){
        spuInfoService.up(spuId);
        return CommonResult.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public CommonResult update(@RequestBody SpuInfoEntity spuInfo) {
        spuInfoService.updateById(spuInfo);

        return CommonResult.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public CommonResult delete(@RequestBody Long[] ids) {
        spuInfoService.removeByIds(Arrays.asList(ids));

        return CommonResult.success();
    }

}
